package com.gm.wholesale.module.webview.mainWebView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;

import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseactivity.BaseWebViewFragment;
import com.gm.commonlib.util.jsonUtils.JsonUtils;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.order.bean.WebViewUseBean;
import com.gm.wholesale.module.order.view.PayMoneyView;
import com.gm.wholesale.module.report.view.ShowCustomReportView;
import com.gm.wholesale.module.user.UserDataManger;
import com.gm.wholesale.module.wx.bean.ShareHtmlBean;
import com.gm.wholesale.module.wx.share.WXShareUtil;
import com.gm.wholesale.view.HeaderView;
import com.youth.banner.util.LogUtils;

import java.util.HashMap;

public class MainWebFragment extends BaseWebViewFragment {
    public static final String key = "url";
    private HeaderView mHeaderView; //顶部header控件

    public void setHeaderView(HeaderView headerView) {
        mHeaderView = headerView;
    }

    public void loadUrl(String url) {
        mViewView.clearCache(true);
        url = getUrl(url);
        LogUtils.d("loadUrl： " + url);
        super.loadUrl(url);
    }


    public void showTitle() {
        if (mHeaderView != null) {
            mHeaderView.setVisibility(View.VISIBLE);
        }
    }

    public void hidTitle() {
        if (mHeaderView != null) {
            mHeaderView.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        super.onCreateContentBody(parent);
        setUserAgent(Constant.SONIC_ANDROID_APP);
        Bundle data = getArguments();
        if (data != null) {
            loadUrl(getUrl(data.getString(key)));
        }
        bindJs();

    }



    public void updateCustom(int type,CustomBean customBean){
        MainActivity activity= (MainActivity) getActivity();
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                activity.updateCustom(type,customBean);
            }
        });
    }


    public void showCustomReport(CustomBean customBean) {
        mActivity.runOnUiThread(() -> {
            ShowCustomReportView showCustomReportView = new ShowCustomReportView(getActivity());
            showCustomReportView.showCustom(customBean, (IBaseView) getActivity());
            Popupwindow.showPopCenterView(getActivity(), showCustomReportView);
        });
    }


    public void showGoodReport(GoodBean goodBean) {
        mActivity.runOnUiThread(() -> {
            ShowCustomReportView showCustomReportView = new ShowCustomReportView(getActivity());
            showCustomReportView.showGoodReport(goodBean, (IBaseView) getActivity());
            Popupwindow.showPopCenterView(getActivity(), showCustomReportView);
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHeaderView=null;
    }

    public void showPayView(WebViewUseBean webViewUseBean) {
        mActivity.runOnUiThread(() -> {
            PayMoneyView payMoneyView = new PayMoneyView(getActivity());
            payMoneyView.showViewByWebViewUse(webViewUseBean);
            payMoneyView.setOnSubmitClickListener((payInfoBean, goodBeans) -> {
                HashMap<String, Object> data = new HashMap<>();
                data.put("payment", payInfoBean.getPayment());
                data.put("paidAmount", payInfoBean.getPaidAmount());
                data.put("discountAmount", payInfoBean.getDiscountAmount());
                String datas = JsonUtils.toString(data);
                String result = "javascript:onReceiveOrderPayNumberKeyboard('" + datas + "')";
                mViewView.evaluateJavascript(result, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String s) {

                    }
                });
            });
            Popupwindow.showPopCenterView(getActivity(), payMoneyView);
        });
    }
    //邀请客户下单
    public void shareWx(ShareHtmlBean bean){
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                WXShareUtil.sendHtml(getActivity(),bean);
            }
        });
    }


    private void bindJs() {
        mViewView.addJavascriptInterface(new MainWebViewCallBack(this), Constant.APP);
    }

    private String getUrl(String url) {
        String token=UserDataManger.newInstance().getToken();
        if (url==null){
            return "";
        }
        if (url.contains("?")){
            return url+"&token="+token;
        }
        return url+"?token="+token;
    }

}
